﻿
#region UsingDirectives
using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.Data;
using System.Resources;
using System.Reflection;
using System.Globalization;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.ApplicationBlocks.ExceptionManagement;
using Microsoft.Communications.Sdp.UI.DataAccess.Properties;
using CSF.Communications.Sdp.ServiceLogic.Exceptions;


#endregion

namespace CSF.Communications.Sdp.ServiceLogic.DataAccess
{
    #region DataServiceProviderClass
    /// <summary>
    /// SyncAsync DataServiceProvider Class
    /// </summary>
    public class DataServiceProvider
    {
        #region Private Members
        private readonly string connectionString;
        private readonly string SBEconnectionString;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor which assigns the connection string.
        /// </summary>
        public DataServiceProvider()
        {
              connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString.ToString();
              SBEconnectionString = ConfigurationManager.ConnectionStrings["SBEDB"].ConnectionString.ToString();
        }
        #endregion

        #region Public Methods

        #region GetDynamicMapfromDatabase
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapname"></param>
        /// <param name="mapType"></param>
        /// <returns></returns>
        public DataSet GetDynamicMappingfromDatabase(string mapname, out string TransformationXml, out int IsComplex)
        {
            string getPAyLoadDetailsSP = "ps_s_GetDynamicMapping";
            DataSet ds = null;
            SqlParameter[] parameterArray;
            try
            {
                parameterArray = new SqlParameter[3];
                parameterArray[0] = new SqlParameter("@MapName", SqlDbType.VarChar, 50);
                parameterArray[0].Value = mapname;
                parameterArray[1] = new SqlParameter("@IsComplex", SqlDbType.Decimal);
                parameterArray[1].Direction = ParameterDirection.Output;
                parameterArray[2] = new SqlParameter("@TransformationXml", SqlDbType.VarChar, 5000);
                parameterArray[2].Direction = ParameterDirection.Output;
                ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, getPAyLoadDetailsSP, parameterArray);
                IsComplex = Convert.ToInt16(parameterArray[1].Value.ToString());
                TransformationXml = parameterArray[2].Value.ToString();
                return ds;

            }
            catch (Exception dataServiceProviderException)
            {
                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorInsertingServiceLogicPayload));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;

            }
        }
        #endregion

        #region InsertSLPayLoad
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", MessageId = "3#")]
        public void InsertSLPayLoad(string sessionId, string corelationId, string midXml, string rulePolicy)
        {
            string InsertPayloadSP = Resources.InsertPayloadSP;
            SqlParameter[] parameterArray;

            try
            {
                parameterArray = new SqlParameter[4];
                parameterArray[0] = new SqlParameter("@CorrelationID", SqlDbType.VarChar, 50);
                parameterArray[0].Value = corelationId;
                parameterArray[1] = new SqlParameter("@SessionID", SqlDbType.VarChar, 50);
                parameterArray[1].Value = sessionId;
                parameterArray[2] = new SqlParameter("@MidXml", SqlDbType.Text);
                parameterArray[2].Value = midXml;
                parameterArray[3] = new SqlParameter("@RulePolicy", SqlDbType.VarChar, 50);
                parameterArray[3].Value = rulePolicy;

                SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, InsertPayloadSP, parameterArray);
            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorInsertingServiceLogicPayload));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
            }

        }
        #endregion

        #region UpdateMID
        public void UpdateMID(string correlationId, string midXml)
        {
            string updateRequestSP = Resources.UpdateServiceLogicStatusSP;

            SqlParameter[] parameterArray;
            try
            {
                parameterArray = new SqlParameter[2];
                parameterArray[0] = new SqlParameter("@CorrelationID", SqlDbType.VarChar, 50);
                parameterArray[0].Value = correlationId;
                parameterArray[1] = new SqlParameter("@MidXml", SqlDbType.NText);
                parameterArray[1].Value = midXml;
                SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, updateRequestSP, parameterArray);
            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorUpdatingMid));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
            }
        }
        #endregion

        #region UpdateMIDArchive
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sessionID"></param>
        /// <param name="midXml"></param>
        public void UpdateMIDArchive(string sessionId, string midXml)
        {
            string updateRequestSP = Resources.UpdateMidArchive;
            SqlParameter[] parameterArray;
            try
            {
                parameterArray = new SqlParameter[2];
                parameterArray[0] = new SqlParameter("@SessionID", SqlDbType.VarChar, 50);
                parameterArray[0].Value = sessionId;
                parameterArray[1] = new SqlParameter("@Middata", SqlDbType.NText);
                parameterArray[1].Value = midXml;
                SqlHelper.ExecuteNonQuery(SBEconnectionString, CommandType.StoredProcedure, updateRequestSP, parameterArray);
            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorArchivingMid));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
            }
        }
        #endregion

        #region GetMid
        public DataSet GetMid(string corelationId)
        {
            string getPAyLoadDetailsSP = Resources.GetPayLoadDetailsSP;
            DataSet ds = null;
            SqlParameter[] parameterArray;
            try
            {
                parameterArray = new SqlParameter[1];
                parameterArray[0] = new SqlParameter("@CorrelationID", SqlDbType.VarChar, 50);
                parameterArray[0].Value = corelationId;
                ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, getPAyLoadDetailsSP, parameterArray);
                return ds;
            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorFetchingMid));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
                ds = null;
            }
        }
        #endregion

        #region DeleteMid
        public DataSet DeleteMid(string corelationId)
        {
            string getPAyLoadDetailsSP = Resources.DeletePayloadSP;
            DataSet ds = null;
            SqlParameter[] parameterArray;
            try
            {
                parameterArray = new SqlParameter[1];
                parameterArray[0] = new SqlParameter("@CorrelationID", SqlDbType.VarChar, 50);
                parameterArray[0].Value = corelationId;
                ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, getPAyLoadDetailsSP, parameterArray);
                return ds;
            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorDeletingMid));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
                ds = null;
            }
        }
        #endregion

        #region ChangeServiceLogicState
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sessionID"></param>
        /// <param name="state"></param>
        public void ChangeServiceLogicState(string sessionId, string requestuuid, int state)
        {

            string ChangeServiceLogicStateSP = String.Empty;
            SqlParameter[] parameterArray;
            try
            {


                if (state == 99)
                {
                    ChangeServiceLogicStateSP = Resources.ChangeServiceLogicStateCompleted;
                }
                if (state == 1)
                {
                    ChangeServiceLogicStateSP = Resources.ChangeServiceLogicStateStared;
                }
                if (state == -1)
                {
                    ChangeServiceLogicStateSP = Resources.ChangeServiceLogicStateTimedOut;
                }
                if (state == 100)
                {
                    ChangeServiceLogicStateSP = Resources.ChangeStateError;
                    parameterArray = new SqlParameter[1];
                    parameterArray[0] = new SqlParameter("@RequestUUID", SqlDbType.VarChar, 50);
                    parameterArray[0].Value = requestuuid;

                }
                else
                {
                    parameterArray = new SqlParameter[1];
                    parameterArray[0] = new SqlParameter("@SessionID", SqlDbType.VarChar, 50);
                    parameterArray[0].Value = sessionId;
                }

                SqlHelper.ExecuteNonQuery(SBEconnectionString, CommandType.StoredProcedure, ChangeServiceLogicStateSP, parameterArray);
            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorUpdatingServicelogicStatus));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
            }



        }
        #endregion

        #region GetProductActionRulePolicy
        /// <summary>
        /// 
        /// </summary>
        /// <param name="productUUID"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", MessageId = "0#")]
        public string GetProductActionRulePolicy(string productUUID, string action)
        {

            string GetRulePolicySP = String.Empty;
            SqlParameter[] parameterArray;
            string rulePolicyName = string.Empty;
            try
            {
                GetRulePolicySP = Resources.GetProductActionRulePolicy;
                parameterArray = new SqlParameter[3];
                parameterArray[0] = new SqlParameter("@ProductId", SqlDbType.NVarChar, 127);
                parameterArray[0].Value = productUUID;
                parameterArray[1] = new SqlParameter("@ActionName", SqlDbType.NVarChar, 127);
                parameterArray[1].Value = action;
                parameterArray[2] = new SqlParameter("@PolicyName", SqlDbType.NVarChar, 127);
                parameterArray[2].Direction = ParameterDirection.Output;

                SqlHelper.ExecuteNonQuery(SBEconnectionString, CommandType.StoredProcedure, GetRulePolicySP, parameterArray);

                rulePolicyName = parameterArray[2].Value.ToString();
                return rulePolicyName;

            }
            catch (Exception dataServiceProviderException)
            {
                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorQueryingSBERulesPolicyTableInDB));
                throw new ApplicationException(dataServiceProviderException.Message);
            }
            finally
            {
                parameterArray = null;
            }

        }
        #endregion

        #region GetIOMapfromDatabase
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapname"></param>
        /// <param name="mapType"></param>
        /// <returns></returns>
        public string GetIOMapfromDatabase(string serviceUUID, string action)
        {

            string getIOMapSP = String.Empty;
            SqlParameter[] parameterArray;
            DataSet ds = null;
            string map = string.Empty;
            try
            {
                getIOMapSP = Resources.GetIOMap;
                parameterArray = new SqlParameter[2];
                parameterArray[0] = new SqlParameter("@ServiceUUID", SqlDbType.NVarChar, 50);
                parameterArray[0].Value = serviceUUID;
                parameterArray[1] = new SqlParameter("@Action", SqlDbType.NVarChar, 50);
                parameterArray[1].Value = action;

                ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, getIOMapSP, parameterArray);

                map = ds.Tables[0].Rows[0][0].ToString();
                return map;

            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorFetchingIO));
                throw new ApplicationException(dataServiceProviderException.Message);

            }
            finally
            {
                parameterArray = null;
            }

        }
        #endregion

        #region GetDynamicMapfromDatabase
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapname"></param>
        /// <param name="mapType"></param>
        /// <returns></returns>
        public string GetDynamicMapfromDatabase(string mapname)
        {

            string getDynamicMapSP = String.Empty;
            SqlParameter[] parameterArray;
            DataSet ds = null;
            string map = string.Empty;
            try
            {
                getDynamicMapSP = Resources.GetDynamicMap;
                parameterArray = new SqlParameter[3];
                parameterArray[0] = new SqlParameter("@MapName", SqlDbType.NVarChar, 50);
                parameterArray[0].Value = mapname;

                ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, getDynamicMapSP, parameterArray);

                map = ds.Tables[0].Rows[0][0].ToString();
                return map;

            }
            catch (Exception dataServiceProviderException)
            {

                ExceptionManager.Publish(dataServiceProviderException, Convert.ToInt32(SDPDataError.ErrorFetchingDynamicMap));
                throw new ApplicationException(dataServiceProviderException.Message);

            }

            finally
            {
                parameterArray = null;
            }

        }


    }
        #endregion





        #endregion
}
    #endregion

